<?php

class Somi_Plugin_Predispatch extends Zend_Controller_Plugin_Abstract
      {
       // Plugin is out of Scope
        private $_isDispatched = false;
        
        // takes the request from the controller
        public function preDispatch(Zend_Controller_Request_Abstract $request){
            if(!$this->_isDispatched){
                $module = $request->getModuleName();
                $resource = $request->getControllerName();
                $action = $request->getActionName();
                if(( $action != 'login' || $_SESSION['LastAction']['action']=='login')&& ( $action != 'passwortvergessen' || $_SESSION['LastAction']['action']=='passwortvergessen') && $_SESSION['ThisAction']['action']!=$action && !$this->_request->isPost() ){
                if(isset($_SESSION['ThisAction'])){
                    $_SESSION['LastAction'] = $_SESSION['ThisAction'];
                }
                }
                $_SESSION['ThisAction'] = $request->getParams();    // array('module' => value, 'controller' => value, 'action' => value, 'param1' => value, 'param2' => value)
                                        
                if(!isset($_SESSION['LastAction'])){
                    $_SESSION['LastAction'] = $_SESSION['ThisAction'];
                }
                                        
                $this->_isDispatched = true;
            }
        }
        
        
      	public function redirectBackUrl(){
            $url = array();
            $url[] = $_SESSION['LastAction']['module'];
            unset($_SESSION['LastAction']['module']);
            $url[] = $_SESSION['LastAction']['controller'];
            unset($_SESSION['LastAction']['controller']);
            $url[] = $_SESSION['LastAction']['action'];
            unset($_SESSION['LastAction']['action']);
            
            foreach($_SESSION['LastAction'] as $param => $value){
                $url[] = $param.'/'.$value;
            }
            
            return implode('/', $url);
        } 
          
      }